import { createRouter, createWebHistory } from 'vue-router'
import { useCounterStore } from '@/stores/counter.ts'


const router = createRouter({
	history: createWebHistory(import.meta.env.BASE_URL),
	routes: [
		// {
		//   path: '/',
		//   name: 'home',
		//   component: HomeView,
		// },
		{
			path: '/',
			name: 'home',
			component: () => import('../views/layout/index.vue'),
			redirect: '/index',
			children: [
				{
					path: "/index",
					name: 'Index',
					component: () => import('../views/index/index.vue'),
					meta: {
						title: "首页"
					}
				},
				{
					path: "/userCharge",
					name: 'UserCharge',
					component: () => import('../views/users/Users.vue'),
					meta: {
						title: "用户管理"
					}
				},
				{
					path: "/UserDist",
					name: 'UserDist',
					component: () => import('../views/users/UserDist.vue'),
					meta: {
						title: "用户分布"
					}
				},
				{
					path: "/admin",
					name: "Admin",
					component: () => import('../views/admin/Admin.vue'),
					meta: {
						title: "管理员管理"
					}
				},
				{
					path: "/shop",
					name: "Shop",
					component: () => import("../views/shop/Shop.vue"),
					meta: {
						title: "店铺管理"
					}
				},
				{
					path: "/shopManager",
					name: "ShopManager",
					component: () => import("../views/shop/ShopManager.vue"),
					meta: {
						title: "店长登记信息"
					}
				}
			]
		},
		{
			path: "/login",
			name: 'Login',
			component: () => import('../views/login/login.vue'),
		}
	],
})



router.beforeEach((to, from, next) => {
	const store = useCounterStore()
	if (!store.getLoginAdmin() && to.name !== "Login") {
		next({ name: "Login" })
	} else {
		next()  // 否则继续导航
	}
})
export default router